<template>
  <div>组织架构
    <el-tree :data="depts" :props="defaultProps" class="department-container" :expand-on-click-node="false" default-expand-all>
      <template v-slot="{data}">
        <el-row style="width: 100%;">
          <el-col :span="20"><div style="background: pink">{{ data.name }}</div></el-col>
          <el-col :span="4">
            <div>
              <span class="re-drop">{{ data.managerName }}</span>
              <el-dropdown @command="dropdownTap($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-tree>
    <!-- 子组件 -->
    <AddEdit ref="dialogRef" :is-edit.sync="isEdit" :current-id="currentId" :show-dialog.sync="showDialog" :department-list="departmentList" @addSuccess="department" />
  </div>
</template>

<script>
import { departmentRequest, deleteDepartmentRequest } from '@/api/department.js'
import { transformListToTree } from '@/api/department'
import AddEdit from '@/views/department/components/addEdit.vue'
export default {

  name: 'Department',
  components: { AddEdit },
  data() {
    return {
      departmentList: [], // 部门列表
      depts: [], // 表单列表
      showDialog: false,
      isEdit: false,
      currentId: '', // 点击的当前元素id
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.department()
  },
  methods: {
    // 1.获取部门列表树形展示
    async department() {
      const res = await departmentRequest()
      console.log(res)
      this.depts = transformListToTree(res.data, 0)
      this.departmentList = res.data
    },
    // 2. 判断类型点击
    dropdownTap(command, id) {
      this.currentId = id
      // 添加事件
      if (command === 'add') {
        console.log(111)
        this.isEdit = false
        this.showDialog = true
      } else if (command === 'delete') { // 删除事件
        this.$confirm('确定要删除这个部门吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 发送删除请求
          await deleteDepartmentRequest(id)
          // 刷新列表
          this.department()
          // 删除提示框
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else { // 编辑事件
        this.isEdit = true
        this.showDialog = true
        this.$nextTick(() => {
          // this.$refs.dialogRef.getDepartmentDetail()
        })
      }
    }
  }
}
</script>

<style>
.department-container{
  margin: 30px 50px;
}
.re-drop{
  margin-right: 10px;
}
</style>
